//
//  DCChatTextView.m
//  xmppTest
//
//  Created by striby on 15/12/30.
//  Copyright © 2015年 com.dc. All rights reserved.
//

#import "DCChatTextView.h"

@implementation DCChatTextView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        _backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
        [self addSubview:_backgroundImageView];
        
        _contentView = [[UITextView alloc]initWithFrame:CGRectZero];
        _contentView.backgroundColor=[UIColor clearColor];
        [self addSubview:_contentView];
        self.direction=DCChatTextViewDirectionRight;
        
        _contentView.editable = NO;
        _contentView.dataDetectorTypes = UIDataDetectorTypeAll;
        _contentView.scrollEnabled=NO;
        
        _contentView.font =[UIFont systemFontOfSize:contentTextFontSize];
        
        self.backgroundColor = [UIColor clearColor];
    }
    
    return self;
    
}

-(void)setText:(NSString *)text atLeftTopPoint:(CGPoint)point andRightTopPoint:(CGPoint)rightTopPoint
{
    
    _contentView.text = text;
     
    CGSize size = [_contentView sizeThatFits:CGSizeMake(rightTopPoint.x-point.x-contentTextMargin*2-_constant, MAXFLOAT)];
    
    CGRect frame = _contentView.frame;
    frame.size.height = size.height;
   
    frame.origin.y=contentTextMargin;
    frame.size.width = size.width;
   
    
    if (self.direction==DCChatTextViewDirectionLeft) {

        frame.origin.x=contentTextMargin+_constant;
        
          _contentView.frame = frame;
        
        [self setFrame:CGRectMake(point.x, point.y, _contentView.frame.size.width+2*contentTextMargin+_constant, _contentView.frame.size.height+2*contentTextMargin)];
        
        
    }
    else
    {
        frame.origin.x=contentTextMargin;
        
        _contentView.frame = frame;
        
        if (size.width<rightTopPoint.x-point.x) {
            
            
            [self setFrame:CGRectMake(rightTopPoint.x-(2*contentTextMargin)-size.width-_constant, point.y, size.width+2*contentTextMargin+_constant, size.height+2*contentTextMargin)];

        }
        else
        {

            [self setFrame:CGRectMake(point.x, point.y, _contentView.frame.size.width+2*contentTextMargin+_constant, _contentView.frame.size.height+2*contentTextMargin)];

        }
        
        
        
    
    }
    
   
    
    self.backgroundImageView.frame = self.bounds;
    
}



@end
